﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Models;
using Common;

namespace Dgrk
{
    public class dgrkConver
    {
        #region 美孚协议标注
        public static DeviceInfo MobilmsgToVo(string text)
        {
            DeviceInfo data = new DeviceInfo();
            try
            {
                int index = text.IndexOf('}');
                if (text.IndexOf('}') > 0)
                {
                    //text = text.Substring(index, text.Length - index);
                    //text = text.Replace("thing.event.property.post", "@");
                    //string[] str = text.Split('@');
                    //text = str[0] + "thing.event.property.post\"}";
                    JObject relVo = (JObject)JsonConvert.DeserializeObject(text);
                    if (relVo != null)
                    {
                        string msg = relVo["params"].ToString();
                        Msg vo = JsonConvert.DeserializeObject<Msg>(msg);
                        string sn = vo.topic.Substring(2, vo.topic.Length - 2);
                        data.DeviceMac = sn;
                        data.DeviceId = sn;
                        data.Temp = vo.temp;
                        data.Vis = vo.viscosity;
                        data.Dens = vo.density;
                        data.Dc = vo.dc;
                        data.Ppm = vo.ppm;
                        data.Aw = vo.water_aw;
                        data.Vol = vo.water_vol;
                        data.Fe1 = vo.fe6;
                        data.Fe2 = vo.fe7;
                        data.Fe3 = vo.fe8;
                        data.Fe4 = vo.fe9;
                        data.Fe5 = vo.fe10;
                        data.Fe6 = vo.fe11;
                        data.Fe7 = vo.fe12;
                        data.Fe8 = vo.fe13;
                        data.Fe9 = vo.fe14;
                        data.Fe10 = vo.fe15;
                        data.MFe1 = vo.mfe1;
                        data.MFe2 = vo.mfe2;
                        data.MFe3 = vo.mfe3;
                        data.MFe4 = vo.mfe4;
                        data.MFe5 = vo.mfe5;
                        data.MFe6 = vo.mfe6;
                        data.MFe7 = vo.mfe7;
                        data.MFe8 = vo.mfe8;
                        data.MFe9 = vo.mfe9;
                        data.MFe10 = vo.mfe10;
                        data.NFe1 = vo.nfe6;
                        data.NFe2 = vo.nfe7;
                        data.NFe3 = vo.nfe8;
                        data.NFe4 = vo.nfe9;
                        data.CreateTime = DateTime.Now;
                    }
                }

            }
            catch (Exception ex)
            {
                lLogWrite.WriteLog("解析error:" + ex.ToString());
                data = null;
            }
            return data;
        }

        #endregion
    }
}
